package com.serkol;


import com.github.binarywang.wxpay.exception.WxPayException;
import com.serkol.ams.bean.Result;
import com.serkol.constant.ResultCode;
import com.serkol.exception.AuthException;
import com.serkol.exception.SucsessException;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import jakarta.servlet.http.HttpServletResponse;
import java.sql.SQLSyntaxErrorException;
import java.util.Iterator;
import java.util.List;

@RestControllerAdvice
public class ExceptionRestControllerAdvice {
    @ExceptionHandler(BindException.class)
    public Result bindExceptionHandler(BindException ex) {
        ex.printStackTrace();
        StringBuffer buffer = new StringBuffer();
        List<ObjectError> allErrors = ex.getAllErrors();
        Iterator<ObjectError> iterator = allErrors.iterator();
        while (iterator.hasNext()) {
            ObjectError next = iterator.next();
            buffer.append(next.getDefaultMessage());
            buffer.append("\n");
        }
        //  FieldError fieldError = ex.getFieldError();
        return Result.failure(buffer.toString());
    }
    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerExceptionHandler(NullPointerException ex) {
        ex.printStackTrace();

        return Result.code(ResultCode.OK_CONTENT_PARTIAL);
    }    @ExceptionHandler(SucsessException.class)
    public Result sucsessExceptionHandler(SucsessException ex) {
        System.out.println(ex.getMessage());
        System.out.println("----------------------SucsessException---START---------------------"  );
        System.out.println("Code:"+ex.getError());
        ex.printStackTrace();

        return Result.code(ResultCode.OK_CONTENT_PARTIAL);
    }

    @ExceptionHandler(SQLSyntaxErrorException.class)
    public Result nullSQLSyntaxErrorException(SQLSyntaxErrorException ex) {
        ex.printStackTrace();

        return Result.code(ResultCode.SERVER_ERROR_BAD_GATEWAY);
    }
    @ExceptionHandler(WxPayException.class)
    public Result WxPayExceptionHandler(WxPayException ex) {
        ex.printStackTrace();


        return Result.failure(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED.getCode(),ex.getErrCodeDes());
    }

    //WxErrorException
    @ExceptionHandler(WxErrorException.class)
    public Result WxErrorExceptionHandler(WxErrorException ex) {
        ex.printStackTrace();
        return Result.failure(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED.getCode(),ex.getMessage());
    }

    @ExceptionHandler(AuthException.class)
    public Result AuthExceptionHandler(HttpServletResponse response, AuthException ex) {
        ex.printStackTrace();
        ex.fillInStackTrace() ;


        int code = ex.getCode();
      //  response.setStatus(code);
        System.out.println("----------------------AuthException---START---------------------");
        String message = ex.getMessage();
        System.out.println("Code:"+code);
        System.out.println("message:"+message);
        Result failure = Result.failure(code,message);
     //   System.out.println(failure.toString());
        ex.printStackTrace();
        System.out.println("----------------------AuthException---END---------------------");
        return failure;
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public Result paramExceptionHandler(MethodArgumentNotValidException e) {
        BindingResult exceptions = e.getBindingResult();
        // 判断异常中是否有错误信息，如果存在就使用异常中的消息，否则使用默认消息
        if (exceptions.hasErrors()) {
            List errors = exceptions.getAllErrors();
            if (!errors.isEmpty()) {// 这里列出了全部错误参数，按正常逻辑，只需要第一条错误即可
                FieldError fieldError = (FieldError) errors.get(0);
                return Result.failure(fieldError.getDefaultMessage());
            } else {
                return Result.failure();
            }
        } else {
            return Result.failure();
        }

    }

    @ExceptionHandler({MissingServletRequestParameterException.class})
    public Result reqParamExceptionHandler(MissingServletRequestParameterException e) {
        e.printStackTrace();


        return Result.failure(e.getMessage());


    }

}
